package com.bartender.ingredient.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.bartender.common.core.domain.entity.SysUser;
import com.bartender.common.core.domain.model.LoginUser;
import com.bartender.common.utils.SecurityUtils;
import com.bartender.ingredient.service.IDifyService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.bartender.common.annotation.Log;
import com.bartender.common.core.controller.BaseController;
import com.bartender.common.core.domain.AjaxResult;
import com.bartender.common.enums.BusinessType;
import com.bartender.ingredient.domain.Ingredient;
import com.bartender.ingredient.service.IIngredientService;
import com.bartender.common.utils.poi.ExcelUtil;
import com.bartender.common.core.page.TableDataInfo;


/**
 * 原料管理Controller
 * 
 * @author bartender
 * @date 2024-11-22
 */
@RestController
@RequestMapping("/ingredient/ingredient")
public class IngredientController extends BaseController
{
    @Autowired
    private IIngredientService ingredientService;

    @Autowired
    private IDifyService difyService;

    /**
     * 查询原料管理列表
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:list')")
    @GetMapping("/list")
    public TableDataInfo list(Ingredient ingredient)
    {
        startPage();
        List<Ingredient> list = ingredientService.selectIngredientList(ingredient);
        return getDataTable(list);
    }

    @GetMapping("/list_front")
    public List<Ingredient> listFront(Ingredient ingredient, @RequestParam Long deptId)
    {
        ingredient.setDeptId(deptId);
        List<Ingredient> list = ingredientService.selectIngredientListFront(ingredient);
        return list;
    }

    /**
     * 导出原料管理列表
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:export')")
    @Log(title = "原料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Ingredient ingredient)
    {
        List<Ingredient> list = ingredientService.selectIngredientList(ingredient);
        ExcelUtil<Ingredient> util = new ExcelUtil<Ingredient>(Ingredient.class);
        util.exportExcel(response, list, "原料管理数据");
    }

    /**
     * 获取原料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:query')")
    @GetMapping(value = "/{ingredientId}")
    public AjaxResult getInfo(@PathVariable("ingredientId") Long ingredientId)
    {
        return success(ingredientService.selectIngredientByIngredientId(ingredientId));
    }

    /**
     * 新增原料管理
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:add')")
    @Log(title = "原料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Ingredient ingredient)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser currentUser = loginUser.getUser();
        // 获取当前登录用户
        Long deptId = currentUser.getDeptId(); // 获取当前用户的部门ID
        ingredient.setDeptId(deptId); // 设置部门ID到对象中
        String name = ingredient.getIngredientName();
        String color = difyService.getColor(name);
        if (color == null || color.equals("")) {
            return AjaxResult.warn("新增失败，请重新点击新增按钮");
        }
        ingredient.setColor(color);
//        return AjaxResult.warn("新增失败，请重新点击新增按钮");
        return toAjax(ingredientService.insertIngredient(ingredient));
    }

    /**
     * 修改原料管理
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:edit')")
    @Log(title = "原料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Ingredient ingredient)
    {
        String name = ingredient.getIngredientName();
        String color = difyService.getColor(name);
        if (color == null || color.equals("")) {
            return AjaxResult.warn("新增失败，请重新点击新增按钮");
        }
        ingredient.setColor(color);
        return toAjax(ingredientService.updateIngredient(ingredient));
    }

    /**
     * 删除原料管理
     */
    @PreAuthorize("@ss.hasPermi('ingredient:ingredient:remove')")
    @Log(title = "原料管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ingredientIds}")
    public AjaxResult remove(@PathVariable Long[] ingredientIds)
    {
        return toAjax(ingredientService.deleteIngredientByIngredientIds(ingredientIds));
    }
}
